package com.mkz.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mkz.admin.entity.SysRole;
import com.mkz.admin.entity.SysUserRole;
import com.mkz.admin.service.SysRoleMenuService;
import com.mkz.admin.service.SysRoleService;
import com.mkz.admin.service.SysUserRoleService;
import com.mkz.admin.vo.RoleVo;
import com.mkz.core.common.constant.CommonConstants;
import com.mkz.core.common.util.ResponseResult;
import com.mkz.core.log.annotation.SysLog;
import com.mkz.jbpm.feign.ActiIdentityClient;

import cn.hutool.core.collection.CollectionUtil;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import javax.validation.Valid;

/**
 * @author XXXXXX
 * @date 2020/3/29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
@Api(value = "role", tags = "角色管理模块")
public class RoleController {
	private final SysRoleService sysRoleService;
	private final SysRoleMenuService sysRoleMenuService;
	private final SysUserRoleService sysUserRoleService;
	private final ActiIdentityClient actiIdentityService;

	/**
	 * 通过ID查询角色信息
	 *
	 * @param id ID
	 * @return 角色信息
	 */
	@GetMapping("/{id}")
	public ResponseResult getById(@PathVariable Integer id) {
		return ResponseResult.ok(sysRoleService.getById(id));
	}

	/**
	 * 添加角色
	 *
	 * @param sysRole 角色信息
	 * @return success、false
	 */
	@SysLog("添加角色")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_role_add')")
	public ResponseResult save(@Valid @RequestBody SysRole sysRole) {
		return ResponseResult.ok(sysRoleService.save(sysRole));
	}

	/**
	 * 修改角色
	 *
	 * @param sysRole 角色信息
	 * @return success/false
	 */
	@SysLog("修改角色")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_role_edit')")
	public ResponseResult update(@Valid @RequestBody SysRole sysRole) {
		return ResponseResult.ok(sysRoleService.updateById(sysRole));
	}

	/**
	 * 删除角色
	 *
	 * @param id
	 * @return
	 */
	@SysLog("删除角色")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_role_del')")
	public ResponseResult removeById(@PathVariable Integer id) {
		return ResponseResult.ok(sysRoleService.removeRoleById(id));
	}

	/**
	 * 同步角色到JBPM模块
	 *
	 * @param id
	 * @return
	 */
	@SysLog("删除角色")
	@PutMapping("/syn/{id}")
	@PreAuthorize("@pms.hasPermission('sys_role_syn')")
	public ResponseResult synRole(@PathVariable Integer id) {
		if(null==id) {
			return ResponseResult.failed("参数错误，"+CommonConstants.ERROR_SHOW_TEXT);
		}
		SysRole sr = sysRoleService.getById(id);
		if(null==sr) {
			return ResponseResult.failed("记录已被删除，"+CommonConstants.ERROR_SHOW_TEXT);
		}
		sr.setSynFlag("1");
		sysRoleService.updateById(sr);
		List<SysUserRole> list = sysUserRoleService.listJbpmByRoleId(id);
		String urrJson = "[]";
		if(CollectionUtil.isNotEmpty(list)) {
			urrJson = JSONObject.toJSONString(list);
		}
		return ResponseResult.ok(actiIdentityService.saveGroup(sr.getRoleCode(), sr.getRoleName(), urrJson));
	}

	/**
	 * 获取角色列表
	 *
	 * @return 角色列表
	 */
	@GetMapping("/list")
	public ResponseResult listRoles() {
		return ResponseResult.ok(sysRoleService.list(Wrappers.emptyWrapper()));
	}

	/**
	 * 分页查询角色信息
	 *
	 * @param page 分页对象
	 * @return 分页对象
	 */
	@GetMapping("/page")
	public ResponseResult getRolePage(Page page) {
		return ResponseResult.ok(sysRoleService.page(page, Wrappers.emptyWrapper()));
	}

	/**
	 * 更新角色菜单
	 *
	 * @param roleVo 角色对象
	 * @return success、false
	 */
	@SysLog("更新角色菜单")
	@PutMapping("/menu")
	@PreAuthorize("@pms.hasPermission('sys_role_perm')")
	public ResponseResult saveRoleMenus(@RequestBody RoleVo roleVo) {
		SysRole sysRole = sysRoleService.getById(roleVo.getRoleId());
		return ResponseResult.ok(sysRoleMenuService.saveRoleMenus(sysRole.getRoleCode()
			, roleVo.getRoleId(), roleVo.getMenuIds()));
	}
}
